package com.school.service.impl;

import com.github.pagehelper.PageInfo;
import com.school.dao.SignMapper;
import com.school.dao.SignWithUserMapper;
import com.school.dto.SignWithUser;
import com.school.model.Sign;
import com.school.model.SignExample;
import com.school.model.User;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class SignServiceImpl {
    @Autowired
    UserServiceImpl userService;
    @Resource
    private SignMapper signMapper;
    @Resource
    private SignWithUserMapper signWithUserMapper;

    public void deleteByUserId(Integer userId) {
        Sign sign = new Sign();
        sign.setSignUserId(userId);
        sign.setDeleted((byte) 1);
        SignExample signExample = new SignExample();
        SignExample.Criteria criteria = signExample.createCriteria();
        criteria.andSignUserIdEqualTo(userId);
        signMapper.updateByExampleSelective(sign, signExample);

        sign = new Sign();
        sign.setSignedUserId(userId);
        sign.setDeleted((byte) 1);
        signExample = new SignExample();
        criteria = signExample.createCriteria();
        criteria.andSignedUserIdEqualTo(userId);
        signMapper.updateByExampleSelective(sign, signExample);
    }


    public List<Sign> queryBySignUserId(Integer likeUserId) {
        SignExample signExample = new SignExample();
        SignExample.Criteria criteria = signExample.createCriteria();
        criteria.andDeletedEqualTo((byte) 0);
        criteria.andSignUserIdEqualTo(likeUserId);
        return signMapper.selectByExampleSelective(signExample);
    }

    public List<Sign> queryBySignedUserId(Integer likedUserId) {
        SignExample signExample = new SignExample();
        SignExample.Criteria criteria = signExample.createCriteria();
        criteria.andDeletedEqualTo((byte) 0);
        criteria.andSignedUserIdEqualTo(likedUserId);
        return signMapper.selectByExampleSelective(signExample);
    }

    public List<Sign> queryBySignUserAndSignedUserId(Integer likeUserId, Integer likedUserId) {
        SignExample signExample = new SignExample();
        SignExample.Criteria criteria = signExample.createCriteria();
        criteria.andDeletedEqualTo((byte) 0);
        criteria.andSignedUserIdEqualTo(likedUserId);
        criteria.andSignUserIdEqualTo(likeUserId);
        return signMapper.selectByExampleSelective(signExample);
    }

    public void add(Sign sign) {
        sign.setAddTime(LocalDateTime.now());
        sign.setUpdateTime(LocalDateTime.now());
        this.signMapper.insertSelective(sign);
    }

    public List<SignWithUser> querySelective(Integer year, String schoolName, Integer page, Integer pageSize, String sort, String order) {
        List<SignWithUser> signWithUsers = signWithUserMapper.querySelective(year, schoolName, page, pageSize, sort, order);
        return signWithUsers;
    }

    public Integer count(Integer year, String schoolName) {
        return signWithUserMapper.count(year, schoolName);
    }

    public Sign queryById(Integer signId) {
        SignExample signExample = new SignExample();
        SignExample.Criteria criteria = signExample.createCriteria();
        criteria.andDeletedEqualTo((byte) 0);
        criteria.andIdEqualTo(signId);
        List<Sign> signs = signMapper.selectByExampleSelective(signExample);
        return signs.size() == 0 ? null : signs.get(0);
    }

    public int updateByIdSelective(Sign sign) {
        sign.setUpdateTime(LocalDateTime.now());
        SignExample signExample = new SignExample();
        return signMapper.updateByPrimaryKeySelective(sign);
    }

    public List<Sign> querySelective(Sign.Column... columns) {
        SignExample signExample = new SignExample();
        SignExample.Criteria criteria = signExample.createCriteria();
        criteria.andDeletedEqualTo((byte) 0);
        List<Sign> signs = this.signMapper.selectByExampleSelective(signExample, columns);
        PageInfo<Sign> signPageInfo = new PageInfo(signs);
        return signPageInfo.getList();
    }

    public Workbook exportSignForm(List<Sign> signs) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        this.prepare(sheet);
        for (int i = 0; i < signs.size(); ++i) {
            Sign sign = (Sign) signs.get(i);
            User signUser = userService.queryById(sign.getSignUserId());
            User signedUser = userService.queryById(sign.getSignedUserId());
            Row row = sheet.createRow(i + 2);
            Cell cell0 = row.createCell(0);
            cell0.setCellValue(signUser.getSchoolName());
            Cell cell1 = row.createCell(1);
            cell1.setCellValue(signedUser.getSchoolName());
            Cell cell2 = row.createCell(2);
            LocalDateTime signTime = sign.getUpdateTime();
            int year = signTime.getYear();
            int month = signTime.getMonth().getValue();
            int dayOfMonth = signTime.getDayOfMonth();
            String time = year + "/" + month + "/" + dayOfMonth;
            cell2.setCellValue(time);
        }
        return workbook;
    }

    private void prepare(Sheet sheet) {
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("签约名单");
        Row row1 = sheet.createRow(1);
        Cell cell1 = row1.createCell(0);
        cell1.setCellValue("学校名称");
        Cell cell2 = row1.createCell(1);
        cell2.setCellValue("签约高校");
        Cell cell3 = row1.createCell(2);
        cell3.setCellValue("签约时间");
    }

    public int deleteById(Integer signId) {
        return signMapper.deleteByPrimaryKey(signId);
    }

}
